﻿<?php

header("Content-Type:text/html; charset=UTF-8");
/*
$referHost=parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST);
$validDomain='localhost';
strstr($referHost,$validDomain)
*/
if(isset($_POST["name"])){
    $ret= array(
        'ok' => false,
        'code'=>0,
        'msg'=>'',

    );
    $db=new mysqli('localhost','root','8japNWTQaTCcS7BP','bitshebm');
    if(mysqli_connect_errno($db)){
        echo mysqli_connect_error();
        exit;
        
    }
    mysqli_query($db, "SET NAMES UTF8");//UTF8数据表，防止乱码
    if(strstr($_SERVER["HTTP_USER_AGENT"], 'QQ/')){
        $ua='QQ';
    }ELSE IF(strstr($_SERVER["HTTP_USER_AGENT"], 'Alipay')){
        $ua='支付宝';
    }ELSE IF(strstr($_SERVER["HTTP_USER_AGENT"], 'MicroMessenger/')){
        $ua='微信';
    }else{
        $ua='其他';
    }
    $bmip = $_SERVER["REMOTE_ADDR"];
    date_default_timezone_set('PRC');
    $date=date('Y-m-d H:i:s',$_SERVER["REQUEST_TIME"]);
    /*-编号
    $result=mysqli_query($db,"SELECT `Id` FROM `bitshebm`.`baoming`");
    $num_rows=0;
    $num_rows=mysqli_num_rows($result);
    -*/
    //是否重复
    $chongfu=$db->prepare("SELECT `姓名`, `班级`, `学号`, `报名日期` FROM `bitshebm`.`baoming` WHERE `学号`=?");
    $chongfu->bind_param("s",$a);
    $a=$_POST["no"];
    $chongfu->execute();
    $chongfu->store_result();

    $shu=$chongfu->num_rows;
    if($shu>0){
        $ret['ok']=false;
        $ret['code']=9;
        $ret['msg']='这个学号已经报名过了';
        $json_string = json_encode($ret);
        echo $json_string;
       /*
        $chongfu->bind_result($cname,$cclass,$cno,$cdate);
        $chongfu->fetch();
        echo "亲爱的同学，您已经于" . $cdate . "报名过" . "</hr>" . "报名信息如下:</hr>&emsp;姓名：" . $cname ."</hr>&emsp;班级：" . $cclass . "</hr>&emsp;学号：" . $cno;
        echo "<hr></hr>如需修改报名信息您可以…………";
        */
        exit;
        
    }
    //合法性检测
    if($_POST["name"]=="" || $_POST["name"]==null){
        $ret['ok']=false;
        $ret['code']=1;
        $ret['msg']='姓名没有填写';
        $json_string = json_encode($ret);
        echo $json_string;
        exit;
    }
    if($_POST["sex"]=="1" ){ }else if($_POST["sex"]=="0" ){}else{
        $ret['ok']=false;
        $ret['code']=2;
        $ret['msg']='性别没有填写';
        $json_string = json_encode($ret);
        echo $json_string;
        exit;
    }
    
    if($_POST["department"]=="" || $_POST["department"]==null){
        $ret['ok']=false;
        $ret['code']=4;
        $ret['msg']='院系没有选择';
        $json_string = json_encode($ret);
        echo $json_string;
        exit;
    }
    if($_POST["major"]=="" || $_POST["major"]==null){
        $ret['ok']=false;
        $ret['code']=5;
        $ret['msg']='专业没有选择';
        $json_string = json_encode($ret);
        echo $json_string;
        exit;
    }
    if($_POST["class"]=="" || $_POST["class"]==null){
        $ret['ok']=false;
        $ret['code']=6;
        $ret['msg']='班级没有选择';
        $json_string = json_encode($ret);
        echo $json_string;
        exit;
    }
    
    $jc=(int)$_POST["no"];
    if($jc<201160101 || $jc>234170541){
        $ret['ok']=false;
        $ret['code']=3;
        $ret['msg']='学号没有填写';
        $json_string = json_encode($ret);
        echo $json_string;
        exit;
    }
    
    $jc=(int)$_POST["qq"];
    if($jc<100000|| $jc>99999999999){
        $ret['ok']=false;
        $ret['code']=7;
        $ret['msg']='QQ没有填写';
        $json_string = json_encode($ret);
        echo $json_string;
        exit;
    }
    $jc=$_POST["tel"];
    $j1=(float)$jc;
    
    
    if($j1<13000000000 || $j1>99999999999){
        $ret['ok']=false;
        $ret['code']=8;
        $ret['msg']='电话没有填写';
        $json_string = json_encode($ret);
        echo $json_string;
        exit;
    }
    $stmt = $db->prepare("INSERT INTO `bitshebm`.`baoming` (`Id`, `姓名`, `性别`, `院系`, `专业`, `班级`, `学号`, `QQ`, `电话`, `擅长`, `期待`, `报名日期`, `UAIP`, `UAFeature`) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? ,?)");
    $Id=$num_rows+1;
    
    $stmt->bind_param("isssssssssssss",$bianhao,$a,$b,$c,$d,$e,$f,$g,$h,$skill,$expect,$i,$j,$k);
    //$bianhao=$Id;
    $a=$_POST["name"];
    $b=$_POST["sex"];
    $c=$_POST["department"];
    $d=$_POST["major"];
    $e=$_POST["class"];
    $f=$_POST["no"];
    $g=$_POST["qq"];
    $h=$_POST["tel"];
    $skill=$_POST["skill"];
    $expect=$_POST["expect"];
    $i=$date;
    $j=$bmip;
    $k=$ua;
    
    $stmt->execute();
    $stmt->close();
    $db->close();
    $ret['ok']=true;
    $ret['code']=0;
    $ret['msg']='报名成功';
    $json_string = json_encode($ret);
    echo $json_string;
    /*
    echo "<h1>报名信息已经录入，欢迎加入BIT社团！即将转入BIT社团首页！</h1>";
    echo "<html><head><meta http-equiv=\"refresh\" content=3;URL=\"http://bitshe.cn\"></head></html>";

    */
}else{
    Header("HTTP/1.1 303 See Other");
    Header("Location: http://bitshe.cn");
    exit; 
    
}
?>